<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/image.png" alt="logo" />
            <div class="title">杉果云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          @click="onMenuClick"
          :items="items"
        />
      </a-col>
      <a-col flex="180px">
        <a-dropdown>
          <a-space wrap :size="16">
            <a-avatar :src="loginUserStore.loginUser.userAvatar" alt="暂无头像" />
            <div v-if="loginUserStore.loginUser.id">
              {{ loginUserStore.loginUser.userName ?? '无名' }}
            </div>
            <div v-else class="user-login-status">
              <a-button type="primary" href="/user/login">登录</a-button>
            </div>
          </a-space>
          <template #overlay>
            <a-menu v-if="loginUserStore.loginUser.id">
              <a-menu-item>
                <router-link to="/user/info"><IdcardOutlined /> 个人中心 </router-link>
              </a-menu-item>
              <a-menu-item @click="doLogout"> <LogoutOutlined /> 退出登录 </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import {
  HomeOutlined,
  LogoutOutlined,
  IdcardOutlined,
} from '@ant-design/icons-vue'
import { message, type MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '../stores/useLoginUserStore'
import { computed } from 'vue'
import { userLogoutUsingPost } from '../api/yonghu'

const loginUserStore = useLoginUserStore()

const router = useRouter()
const current = ref<string[]>([''])

const onMenuClick = ({ key }) => {
  router.push({
    path: key,
  })
}

router.afterEach((to) => {
  current.value = [to.path]
})

const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0) {
    message.success('退出登录成功')
    loginUserStore.resetLoginUser()
    router.push('/user/login')
  }
}

// 菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
    title: '主页',
  },
  {
    key: '/admin/manage',
    label: '用户管理',
    title: '用户管理',
  },

  {
    key: '/admin/picture_manage',
    label: '图片管理',
    title: '图片管理',
  },
  {
    key: '/add_picture',
    label: '创建图片',
    title: '创建图片',
  },
  {
    key: '/admin/space_manage',
    label: '空间管理',
    title: '空间管理',
  },
]

// 过滤菜单项
const filterMenus = (menus = [] as MenuProps['items']) => {
  return menus?.filter((menu) => {
    if (menu?.key?.startsWith('/admin')) {
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== 'admin') {
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed<MenuProps['items']>(() => filterMenus(originItems))
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
